package msu.erdc.hci;

import java.util.Date;
import java.util.Locale;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import msu.erdc.hci.DateDialogFragment.DateDialogFragmentListener;
import msu.erdc.hci.customObjects.LicenseObject;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class LicenseInfo extends Fragment{
	
	EditText licenseNum;
	Button issueDateButton;
	Button expDateButton;
	EditText state;
	EditText classText;
	EditText restrictions;
	EditText endorsements;
	LicenseObject license;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view =  inflater.inflate(R.layout.license, container, false);
		//do ui stuff here
		
		licenseNum = (EditText) view.findViewById(R.id.licenseNumberText);
		licenseNum.setText(license.licenseNumber);
		
		issueDateButton = (Button) view.findViewById(R.id.issueDateButton);
		issueDateButton.setText(license.issueDate);
		
		issueDateButton.setOnClickListener(new OnClickListener(){
			public void onClick(View v) {
				Calendar date = Calendar.getInstance();
				SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
				Date dateObj = null;
				try {
					dateObj = formatter.parse(license.issueDate);
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					//e.printStackTrace();
				}
				date.setTime(dateObj);
				
				
				DateDialogFragment ddf = DateDialogFragment.newInstance(getActivity(), R.string.license_issue_date, date, true);
				ddf.setDateDialogFragmentListener(new DateDialogFragmentListener() {
				 
				    public void dateDialogFragmentDateSet(Calendar date) {
				    	
				    	String month = String.valueOf(date.get(Calendar.MONTH) + 1);
				    	String day = String.valueOf(date.get(Calendar.DATE));
				    	String year = String.valueOf(date.get(Calendar.YEAR));
				    	
				    	license.issueDate = month + "/" + day + "/" + year;
				    	
				        // update the fragment
				    	issueDateButton.setText(license.issueDate);
				    }
				});
				 
				ddf.show(getFragmentManager(), "date picker dialog fragment");
			}});
		
		expDateButton = (Button) view.findViewById(R.id.expButton);
		expDateButton.setText(license.expirationDate);
		
		expDateButton.setOnClickListener(new OnClickListener(){
			public void onClick(View v) {
				Calendar date = Calendar.getInstance();
				SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
				Date dateObj = null;
				try {
					dateObj = formatter.parse(license.expirationDate);
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					//e.printStackTrace();
				}
				date.setTime(dateObj);
				
				
				DateDialogFragment ddf = DateDialogFragment.newInstance(getActivity(), R.string.license_expiration_date, date, true);
				ddf.setDateDialogFragmentListener(new DateDialogFragmentListener() {
				 
				    public void dateDialogFragmentDateSet(Calendar date) {
				    	
				    	String month = String.valueOf(date.get(Calendar.MONTH) + 1);
				    	String day = String.valueOf(date.get(Calendar.DATE));
				    	String year = String.valueOf(date.get(Calendar.YEAR));
				    	
				    	license.expirationDate = month + "/" + day + "/" + year;
				    	
				        // update the fragment
				    	expDateButton.setText(license.expirationDate);
				    }
				});
				 
				ddf.show(getFragmentManager(), "date picker dialog fragment");
			}});
		
		state = (EditText) view.findViewById(R.id.stateText);
		state.setText(license.state);
		
		classText = (EditText) view.findViewById(R.id.classText);
		classText.setText(license.classString);
		
		restrictions = (EditText) view.findViewById(R.id.restrictText);
		restrictions.setText(license.restriction);
		
		endorsements = (EditText) view.findViewById(R.id.endorseText);
		endorsements.setText(license.endorsements);
		
		return view;
	}	
	

	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        license = new LicenseObject();
	}
}
